package com.kksoft.android.xiaoliaobaike;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import net.tsz.afinal.FinalActivity;
import net.tsz.afinal.FinalDb;
import net.tsz.afinal.annotation.view.ViewInject;
import android.content.Intent;
import android.os.Bundle;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.kksoft.android.xiaoliaobaike.model.Article;
import com.kksoft.android.xiaoliaobaike.model.Series;

public class ArticleListActivity extends FinalActivity {

	int seriesId = 0;

	FinalDb db;

	Series series;
	List<Map<String, Object>> articleMapList;

	@ViewInject(id = R.id.articleLv)
	ListView articleLv;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		super.setContentView(R.layout.activity_article_list);

		seriesId = getIntent().getIntExtra("seriesId", 0);

		if (seriesId == 0) {
			throw new IllegalArgumentException("seriesId can not be null");
		}

		db = FinalDb.create(this, Const.DB_NAME, true);
		series = db.findAllByWhere(Series.class, "seriesId=" + seriesId).get(0);

		setTitle(series.getTitle());

		articleMapList = new ArrayList<Map<String, Object>>();

		SimpleAdapter articleAdapter = new SimpleAdapter(this, articleMapList, R.layout.article_list_item, new String[] { "id", "title", "articleId", "originalUrl", "pubDate" }, new int[] { R.id.id, R.id.title, R.id.articleId, R.id.originalUrl, R.id.pubDate });

		articleLv.setAdapter(articleAdapter);
//		originalUrl
		
		articleLv.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(android.widget.AdapterView<?> parent, android.view.View view, int position, long id) {
				TextView originalUrl = (TextView)parent.findViewById(R.id.originalUrl);
				
				Intent intent = new Intent(ArticleListActivity.this, ArticleActivity.class);
				
				intent.putExtra("originalUrl", originalUrl.getText().toString());
				
				startActivity(intent);
			};
		});
		
//		articleLv.setOnItemLongClickListener(new OnItemLongClickListener() {
//			public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
//				final TextView originalUrl = (TextView) view.findViewById(R.id.originalUrl);
//
//				if (originalUrl.getText() == null || originalUrl.getText().length() == 0) {
//
//				} else {
//					AlertDialog.Builder builder = new AlertDialog.Builder(ArticleListActivity.this);
//					builder.setMessage("查看漫画？");
//					builder.setNeutralButton("打开", new OnClickListener() {
//						public void onClick(DialogInterface dialog, int which) {
//							Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(originalUrl.getText().toString()));
//							startActivity(intent);
//						}
//					});
//					builder.setNegativeButton("放弃", new OnClickListener() {
//						public void onClick(DialogInterface dialog, int which) {
//							dialog.dismiss();
//						}
//					});
//
//					builder.create().show();
//				}
//
//				return true;
//			}
//		});
		
		refresh();
	}

	public void refresh() {
		List<Article> articleList = db.findAllByWhere(Article.class, "seriesId=" + seriesId);
		articleMapList.clear();

		for (Article a : articleList) {
			articleMapList.add(a.toMap());
		}
	}
}